﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using WebCS;
public partial class _Default : System.Web.UI.Page 
{
    WebCS.ImportCS WIcs= new WebCS.ImportCS();
    WebCS.StartCS WScs = new WebCS.StartCS();
 
    string Mard_List = System.Configuration.ConfigurationManager.AppSettings["strcon"];
    WebCS.StartCS Connect = new WebCS.StartCS();
    string Mard_Harmful = System.Configuration.ConfigurationManager.AppSettings["strcon1"];
    WebCS.StartCS Connect1 = new WebCS.StartCS();

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            lblDisplay.Controls.Add(new Sindhu.PagingControl());
            DataLoad();
        }
        if (Session["Err"] != null)
        {
            this.Err.Text = Session["Err"].ToString();
        }
    }
    public void Paging_Click(object sender, CommandEventArgs e)
    {
        string CurrentPage = e.CommandArgument.ToString();
        GVDisplay.PageIndex = Convert.ToInt32(CurrentPage) - 1;
        DataLoad();
    }
    public void DataLoad()
    {
        //DataSet DS = new DataSet();
        //DS.Tables.Add(GetData());

        // in case  of datagrid / gridview
        Paging1.PageSize = GVDisplay.PageSize;
        Paging1.TotalRecord = GetData().Rows.Count;
        Paging1.CurrentPage = GVDisplay.PageIndex + 1;
        Paging1.DataLoad();

        GVDisplay.DataSource = GetData().DefaultView;
        GVDisplay.DataBind();
    }
    public DataTable GetData()
    {
        //string FileUpload = @"C:\Documents and Settings\Administrator\My Documents\Downloads\DanhMucNhaXuatNhapKhau.xls";
        //return WIcs.ExcelToDatatable(FileUpload.ToString(), WIcs.GetExcelSheetNames(FileUpload.ToString())[0]);
        return WScs.subSelectBySQLAdapter("SELECT     MaXNK AS [Mã Nhà XNK], TenXNK AS [Tên Nhà XNK], DiaChi AS [Địa chỉ], DienThoai AS [Điện thoại], Fax, Email " +
"FROM         DMNhaXuatNhapKhau", Mard_List.ToString());
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Import Excel
        System.IO.File.Delete(Server.MapPath(@"~\Import\Test.xls"));
        WebCS.ImportCS WIcs = new ImportCS();
        WebVB.UploadVB WUvb = new WebVB.UploadVB();
        if (WUvb.UploadFile(FileUpload1, Server.MapPath(@"~\Import\Test.xls")) == true)
        {
            WIcs.Import("DMNhaXuatNhapKhau", Server.MapPath(@"~\Import\Test.xls"), WIcs.GetExcelSheetNames(Server.MapPath(@"~\Import\Test.xls"))[0], Mard_List.ToString());
            DataLoad();
            this.Err.Text = "* Upload và Import thành công";
        }
        else
        {
            this.Err.Text = "* Xin vui lòng chọn file để upload";
        }
    }
    
    protected void Button2_Click(object sender, EventArgs e)
    {
        //Export Excel
        WebCS.ExportCS WEcs= new ExportCS();
        System.IO.File.Delete(Server.MapPath(@"~\Export\Test.xls"));
        WEcs.ExportToExcel(WScs.subSelectBySQLAdapter("SELECT     MaXNK AS [Mã Nhà XNK], TenXNK AS [Tên Nhà XNK], DiaChi AS [Địa chỉ], DienThoai AS [Điện thoại], Fax, Email " +
"FROM         DMNhaXuatNhapKhau", Mard_List.ToString()), Server.MapPath(@"~\Export\Test.xls"));
        
        Response.Redirect(Request.Url.ToString().Replace("Default.aspx","Export/Test.xls"));
    }
    private void Nhap()
    {
        if (txtmanhaXNK.Text.ToString() == "" || txttennhaXNK.Text.ToString() == "")
        {
            Session["Err"] = "* Bạn vui lòng nhập Mã và Tên Nhà Sản Xuất";
        }
        else
        {
            if (Connect.ChayCauLenhSQLTraVeGiaTriDungSai("Insert Into DMNhaXuatNhapKhau(MaXNK, TenXNK, Diachi, Dienthoai, Fax, Email, Mota) Values(N'" + txtmanhaXNK.Text.ToString() + "',N'" + txttennhaXNK.Text.Trim() + "',N'" + txtdiachi.Text.Trim() + "',N'" + txtdienthoai.Text.Trim() + "' ,N'" + txtfax.Text.Trim() + "',N'" + txtemail.Text.Trim() + "' ,N'" + txtmota.Text.Trim() + "' )", Mard_List.ToString()) == true)
            {
                Session["Err"]= "* Bạn đã thêm mới 1 bản ghi mới thành công";
            }
            else
            {
                Session["Err"]= "* Xin vui lòng kiểm tra lại dữ liệu nhập";
            }
            DataLoad();
        }
    }

    protected void btnNhap_Click(object sender, EventArgs e)
    {
        Nhap();
        
        Response.Redirect("Default.aspx");
    }
    protected void HyperLink1_Load(object sender, EventArgs e)
    {
    HyperLink1.NavigateUrl=Request.Url.ToString().Replace("Default.aspx", "Download/MauImport DMNhaXuatNhapKhau.rar");
    }
}
